<template>
  <el-dialog :title="info.title" :visible="dialogVisible" width="30%" :before-close="handleClose">
    <strong>请输入类目名称：</strong>
    <el-input v-model="input" placeholder="请输入内容" style="padding-top: 10px;"></el-input>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="SubmitEvent">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props:['type','info'],
  data() {
    return {
      dialogVisible:false,
      input: '',
    }
  },
  methods:{
    handleClose(done){
      // done()
      this.dialogVisible = false
    },
    //提交
    SubmitEvent(){
      if(this.type == 1){ //新增
        this.insertItemCategory({cid:this.info.cid,name:this.input})
      } else if(this.type == 2){ //修改
       this.updateCategory({id:this.info.id,name:this.input})
      } else { //新增一级类目
       this.insertCategory({name:this.input})

      }
    },
    //新增一级类目
    async insertCategory(params){
      let res = await this.$api.insertCategory(params)
      //console.log('新增',res.data);
      if(res.data.status == 200){
        this.dialogVisible = false
        this.$emit('updateview')
        this.$message({
          message: '添加成功',
          type: 'success'
        });
      }
    },
    //新增子类目
    async insertItemCategory(params){
      let res = await this.$api.insertItemCategory(params)
      //console.log('新增',res.data);
      if(res.data.status == 200){
        this.dialogVisible = false
        this.$emit('updateview')
        this.$message({
          message: '添加成功',
          type: 'success'
        });
      }
    },
    //修改类目名称
    async updateCategory(params){
      let res = await this.$api.updateCategory(params)
      //console.log('修改',res.data);
      if(res.data.status == 200){
        this.dialogVisible = false
        this.$emit('updateview')
        // this.$parent.itemCategory() //获取父组件的实例，调用父组件的方法
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      }
    },
  }
}
</script> 

<style scoped>

</style>
